package com.ucar.ui.login;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.ucar.R;
import com.ucar.ui.base.BaseActivity;

public class UserInfoActivity extends BaseActivity implements OnClickListener,
		OnCheckedChangeListener {

	private EditText et_userinfo_fillname;
	private TextView tv_userinfo_province;
	private TextView tv_userinfo_citycode;
	private RadioGroup rg_userinfo_gander_group;
	private Button save_userinfo;
	private String gender;
	private String initname;
	private Context mContext = null;
	String provinceArr[] = { "京", "津", "沪", "渝", "冀", "晋", "辽", "吉", "黑", "苏",
			"浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "琼", "川", "黔", "滇",
			"陕", "甘", "青", "藏", "桂", "蒙", "宁", "新", "港" };
	String provinceArr2[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
			"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
			"X", "Y", "Z" };
	private ArrayList<HashMap<String, Object>> list;
	private PopupWindow popupWindow;
	private GridView gridview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBaseTitleContent(R.layout.activity_userinfo, R.string.userinfo);
		list = new ArrayList<HashMap<String, Object>>();
		showPopupWindowProvince();
	}

	@Override
	public void onInit() {
		setLeftImage(R.drawable.btn_close);
	}

	@Override
	public void onFindViews() {
		et_userinfo_fillname = find(R.id.et_userinfo_fillname);
		tv_userinfo_province = find(R.id.tv_userinfo_province);
		tv_userinfo_citycode = find(R.id.tv_userinfo_citycode);
		rg_userinfo_gander_group = find(R.id.rg_userinfo_gander_group);
		save_userinfo = find(R.id.save_userinfo);

	}

	@Override
	public void onInitViewData() {

	}

	@Override
	public void onBindListener() {
		tv_userinfo_province.setOnClickListener(this);
		tv_userinfo_citycode.setOnClickListener(this);
		save_userinfo.setOnClickListener(this);
		rg_userinfo_gander_group.setOnCheckedChangeListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_userinfo_province:
			popupWindow.showAtLocation(gridview, Gravity.CENTER, 0, 0);
			break;
		case R.id.tv_userinfo_citycode:
			showPopupWindowCitycode();
			break;
		case R.id.save_userinfo:
			saveUserInfo();
			break;
		}
	}

	private void showPopupWindowCitycode() {
		// 显示出城市代码的方法

	}

	private void showPopupWindowProvince() {

		gridview = (GridView) View.inflate(this, R.layout.popupwindow_province,
				null);

		for (int i = 0; i < provinceArr.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("provinceArr", provinceArr[i]);
			list.add(map);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
				R.layout.gridview_item, new String[] { "provinceArr" },
				new int[] { R.id.tv_province });
		// 设置GridView的适配器为新建的simpleAdapter
		gridview.setAdapter(simpleAdapter);

		popupWindow = new PopupWindow(getLayoutInflater().inflate(
				R.layout.popupwindow_province, null),
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
		popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setContentView(gridview);
	}

	private void saveUserInfo() {
		initname = et_userinfo_fillname.getText().toString().trim();

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// 获取变更后的选中项的ID
		int radioButtonId = group.getCheckedRadioButtonId();
		// 根据ID获取RadioButton的实例
		RadioButton rb = (RadioButton) UserInfoActivity.this
				.findViewById(radioButtonId);
		// 更新文本内容，以符合选中项
		gender = (String) rb.getText();
		mSpUtil.setGender(gender);
	}
}
